package com.example.server.controller;

import com.example.server.pojo.LoginJson;
import com.example.server.pojo.MyUser;
import com.example.server.pojo.RespBean;
import com.example.server.service.MyUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/api")
public class LoginCtl {
    @Autowired
    private MyUserService myUserService;

    @ApiOperation(value = "登录验证")
    @PostMapping("/login")
    public RespBean login(@RequestParam("username") String user,
                          @RequestParam("password") String password){
        MyUser myuser = myUserService.login(user, password);
        log.info("-----结果："+myuser);

        if (myuser != null){
            return new RespBean(200,"success",myuser);
        }else {
            return new RespBean(400,"fail",null);
        }
    }

    @ApiOperation(value = "json登录验证")
    @PostMapping("/loginjson")
    public RespBean loginJson(@RequestBody LoginJson loginJson){
        log.info("收到的参数：username: {}, password: {}",
                loginJson.getUsername(),
                loginJson.getPassword());
        MyUser myuser = myUserService.login(loginJson.getUsername(), loginJson.getPassword());
        myuser.setToken(loginJson.getUsername());
        log.info("-----结果："+myuser);

        if (myuser != null){
            return new RespBean(200,"success",myuser);
        }else {
            return new RespBean(401,"fail",null);
        }
    }
}
